#include "wc3agent.h"
#include "Extern_global_Variable.h"

typedef LongBool (__stdcall *_CAgent_IsValid_1) ();
_CAgent_IsValid_1 _CAgent_IsValid;
typedef procedure (__stdcall *_EnumTypeInstances_1) (const Pointer Param, const LongBool EnumChilds);
_EnumTypeInstances_1 _EnumTypeInstances;

Dword TAgent::GetTypeId()
{
	typedef Dword (*TGetTypeId) (void);

	__asm
	{
		mov ecx, this
	}
	return TGetTypeId((HMODULE)vtable + 0x1C)();
}

Boolean TAgent::IsValid()
{
	__asm
	{
		mov ecx, this
	}
	return _CAgent_IsValid();
}

procedure TAgent::Destroy()
{
	procedure (*CAgent_virtual_Destroy)(void);

	CAgent_virtual_Destroy = ((procedure (*)(void))((HMODULE)vtable + 0x5C));
	__asm
	{
		mov ecx, this
	}
	CAgent_virtual_Destroy();
}

LongBool __stdcall EnumTypeInstances_RawCallback()
{
	PAgent Instance;
	PEnumAgentParam MyParam;

	__asm
	{
		mov Instance,ecx
		mov MyParam,edx
	}

	return (*MyParam).Callback(Instance, (*MyParam).Param);
}

procedure EnumTypeInstances(Dword TypeId, TEnumAgentCallback Callback, Pointer Param, LongBool EnumChilds)
{
	TEnumAgentParam MyParam;

	MyParam.Callback = Callback;
	MyParam.Param = Param;

	__asm
	{
		mov ecx,TypeId
		lea edx,EnumTypeInstances_RawCallback
	}
	_EnumTypeInstances(&MyParam, EnumChilds);
}

procedure w3agent_Init()
{
	Pointer(_CAgent_IsValid) = Pointer(GameDll + 0x0418A0);
	Pointer(_EnumTypeInstances) = Pointer(GameDll + 0x46D1B0);
	Pointer(ppAgileData) = Pointer(GameDll + 0xAB7368);
}

void wc3agent_main()
{
	wc3main_SubscribeOnInit(&w3agent_Init);
}
